package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.DailyDetailData;
import com.yanqu.road.entity.player.UserDailyData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDailyDataDaoImpl extends TempDataObjectDao<UserDailyData> {

    public UserDailyData getUserDailyData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_daily_data where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserDailyData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_daily_data(userId,resetTime,dailyData) values (?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getResetTime());
            call.setString(i++, data.getDailyDetailDataJsonStr());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserDailyData data) {
        PreparedStatement call = null;
        String sql = "update t_u_daily_data set resetTime=?,dailyData=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getResetTime());
            call.setString(i++, data.getDailyDetailDataJsonStr());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserDailyData parseData(ResultSet rs) throws SQLException {
        UserDailyData data = new UserDailyData();
        data.setUserId(rs.getLong("userId"));
        data.setResetTime(rs.getLong("resetTime"));
        String dailyDataStr = rs.getString("dailyData");
        if(StringUtils.isNullOrEmpty(dailyDataStr)){
            dailyDataStr = "{}";
        }
        DailyDetailData dailyDetailData = JSON.parseObject(dailyDataStr, new TypeReference<DailyDetailData>() {});
        data.setDailyDetailData(dailyDetailData);
        return data;
    }
}
